package com.reggie.module.entity.dto.employeeDto;

import com.baomidou.mybatisplus.annotation.TableId;
import com.reggie.module.entity.po.Employee;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Pattern;
import java.time.LocalDateTime;

import static com.reggie.common.Constant.LOGIN_SESSION;

/**
 * @Author 早睡蛋
 * @Date 2022/9/19 16:57:44
 * @Desc:
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class AddEmployeeDto {

    @ApiModelProperty(value = "姓名", required = true)
    @TableId("name")
    @NotEmpty(message = "姓名不能为空")
    @Pattern(regexp = "(^[\\u0391-\\uFFE5]+$)", message = "姓名仅支持中文")
    private String name;

    @ApiModelProperty(value = "用户名", required = true)
    @TableId("username")
    @NotEmpty(message = "用户名不能为空")
    @Pattern(regexp = "((^[A-Za-z0-9]+$))", message = "用户名仅支持中文加数字")
    private String username;

    @ApiModelProperty(value = "电话", required = true)
    @TableId("phone")
    @NotEmpty(message = "电话不能为空")
    @Pattern(regexp = "(^[1](([3][0-9])|([4][0,1,4-9])|([5][0-3,5-9])|([6][2,5,6,7])|([7][0-8])|([8][0-9])|([9][0-3,5-9]))[0-9]{8}$)", message = "手机号格式错误")
    private String phone;

    @ApiModelProperty(value = "性别", required = true)
    @TableId("sex")
    @NotEmpty(message = "性别不能为空")
    @Pattern(regexp = "(^[0-1]*$)", message = "性别：0或者1")
    private String sex;

    @ApiModelProperty(value = "身份证号码", required = true)
    @TableId("idNumber")
    @NotEmpty(message = "身份证号码不能为空")
    @Pattern(regexp = "(^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)", message = "身份证号码格式错误")
    private String idNumber;


    public Employee addEmp(HttpServletRequest request) {
        Employee employee = new Employee();
        employee.setName(this.name);
        employee.setUsername(this.username);
        employee.setPhone(this.phone);
        employee.setSex(this.sex);
        employee.setIdNumber(this.idNumber);
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        Long empId = (Long) request.getSession().getAttribute(LOGIN_SESSION);
        employee.setCreateUser(empId);
        employee.setUpdateUser(empId);
        return employee;
    }

}
